import LoginV from "../view/login.ejs";
import {postLogin} from "../api/admin";
export default ()=>{
	return (req, res, next) => {
		// 接收的参数即是呈现的内容
		res.render(LoginV());
		document.querySelector("#loginBtn").onclick = async function(e){
			e.preventDefault();// 移除默认行为
			const adminName = document.loginForm.adminName.value;
			const password = document.loginForm.password.value;
			if(!/^[a-z]+$/i.test(adminName)){
				toastr.error('请输入正确的管理员账号-只支持英文！');
				return;
			}
			if(!/^\w+$/.test(password)){
				toastr.error("密码只支持数字、字母、下划线");
				return;
			}
			const data = await postLogin({adminName,password});
			toastr.success(data.msg);
			// 存储管理员账号
			localStorage.setItem("adminName",adminName);
			localStorage.setItem("token",data.token);
			location.href = "/index/adminList";
		}
	}
}